package com.ereport.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ereport.entity.Accounts;
import com.ereport.entity.AdminExeBudget;
import com.ereport.entity.DailyActivity;
import com.ereport.util.ApplicationContext;

public class DailyActivityDAO {

static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}

	public int insert(DailyActivity da) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(da);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public List<Accounts> getOrgList(int id) throws Exception {
		List<Accounts> aeb=new ArrayList<Accounts>();
		Session session=startOperations();
		Query qry=session.createQuery("from Accounts where exe_id=:id ");
		qry.setInteger("id", id);
		aeb=qry.list();
		endOperations(session);
		return aeb;
	}

	public List<DailyActivity> getActivity(int id, java.sql.Date edate) throws Exception {
		List<DailyActivity> aeb=new ArrayList<DailyActivity>();
		Session session=startOperations();
		Query qry=session.createQuery("from DailyActivity where exe_id=:id and edate=:edate");
		qry.setInteger("id", id);
		qry.setDate("edate", edate);
		aeb=qry.list();
		endOperations(session);
		return aeb;
	}

	public List<DailyActivity> getWeekActivity(int id, java.sql.Date date,
			java.sql.Date date2) throws Exception {
		List<DailyActivity> aeb=new ArrayList<DailyActivity>();
		Session session=startOperations();
		Query qry=session.createQuery("from DailyActivity where exe_id=:id and edate between '"+date+"' and '"+date2+"'");
		qry.setInteger("id", id);
		aeb=qry.list();
		endOperations(session);
		return aeb;
	}

	public List<DailyActivity> getMnthActivity(int id, String month,String year) throws Exception {
			List<DailyActivity> aeb=new ArrayList<DailyActivity>();
			Session session=startOperations();
			Query qry=session.createQuery("from DailyActivity where exe_id=:id and edate like '"+year+"-"+month+"%'");
			qry.setInteger("id", id);
			aeb=qry.list();
			endOperations(session);
			return aeb;
	}

	public DailyActivity getDailyAct(int daid) throws Exception {
		DailyActivity da=new DailyActivity();
		Session session=startOperations();
		Query qry=session.createQuery("from DailyActivity where id=:id ");
		qry.setInteger("id", daid);
		da=(DailyActivity) qry.uniqueResult();
		endOperations(session);
		return da;
	}
	
	
}
